from symbide import PATHS

import os.path

import gtk

from widgetOrganizer import WidgetOrganizer
from settingChanger import SettingChanger
from monitor import Monitor
from dataLoader import DataLoader
import dialogs


class MainMenu(WidgetOrganizer, gtk.MenuBar):

    def __init__(self, ancestor):
        gtk.MenuBar.__init__(self)
        WidgetOrganizer.__init__(self, 'MainMenu', ancestor)
        
        menus = [Menu_File(self), Menu_Edit(self), Menu_Insert(self), Menu_Help(self)]
        
        for item in menus:
            self.addChild(item)
            self.add(item)


class Submenu(WidgetOrganizer, gtk.MenuItem):
    
    def __init__(self, ancestor, caption, entries):
        gtk.MenuItem.__init__(self, caption)
        WidgetOrganizer.__init__(self, 'Menu_File', ancestor)
        
        self.menu = gtk.Menu()
        self.set_submenu(self.menu)
        
        #i[0]..method Name, i[1]..caption, i[2]..additional parameters
        for i in entries:
            if i[0]=='<seperator />':
                item = gtk.SeparatorMenuItem()
            else:
                item = gtk.MenuItem(i[1], use_underline=False)
                if len(i)==3:
                    item.connect('activate',eval('self._event_'+i[0]), i[2])
                else:
                    item.connect('activate',eval('self._event_'+i[0]))
            item.show()
            self.menu.append(item)


class Menu_File(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                  ('new','New'),
                  ('open','Open'),
                  ('<seperator />', ''),
                  ('save','Save'),
                  ('saveAs','Save as'),
                  ('saveAll','Save all'),
                  ('<seperator />', ''),
                  ('export','Export'),
                  ('<seperator />', ''),
                  ('quit', 'Quit')
                  ]
        Submenu.__init__(self, ancestor, 'File', entries)       
    
    def _event_new(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').addWorksheet()
    
    def _event_open(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').openSheet()
    
    def _event_save(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').saveSheet()
    
    def _event_saveAs(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').saveSheetAs()
    
    def _event_saveAll(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').saveSheet('all')
    
    def _event_export(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').exportSheet()
        
    def _event_quit(self, widget):
        self.getAncestor('MainWindow')._event_quit()
      
      
class Menu_Edit(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                   ('settings','Settings'),
                   ]
        Submenu.__init__(self, ancestor, 'Edit', entries)
        
    def _event_settings(self, widget):
        SettingChanger(self.getAncestor('MainWindow'))
        
class Menu_Insert(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                   ('pythonInput', 'Python Input'),
                   ('sympyInput', 'Sympy Input'),
                   ('textInput', 'Text Input'),
                   ('<seperator />', ''),
                   ('color', 'Color'),
                   ('path', 'Path'),
                   ('load_csv','Load CSV-File'),
                   ]
        Submenu.__init__(self, ancestor, 'Insert', entries)
        
    def _event_load_csv(self, widget):
        DataLoader(self.getAncestor('MainWindow'))
    
    def _event_path(self, widget):
        path = dialogs.select_path()
        if path:
            self.getAncestor('MainWindow').getChildren('Notebook').insertText(path)
        
    def _event_color(self, widget):
        color = dialogs.select_color()
        if color:
            self.getAncestor('MainWindow').getChildren('Notebook').insertText(color.to_string())
    
    def _event_pythonInput(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').insertInput('Python')
        
    def _event_sympyInput(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').insertInput('Sympy')
        
    def _event_textInput(self, widget):
        self.getAncestor('MainWindow').getChildren('Notebook').insertInput('Text')    

class Menu_Help(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                   ('about','About'),
                   ('monitor', 'Monitor')
                   ]
        Submenu.__init__(self, ancestor, 'Help', entries)
        self.menu.append(Menu_Examples(self))
    
    def _event_monitor(self, widget):
        Monitor(self.getAncestor('MainWindow'))
        
    def _event_about(self, widget):
        dialogs.about()
        
class Menu_Examples(Submenu):
    
    def __init__(self, ancestor):
        entries = []
        dir = PATHS['examples']
        for l in os.listdir(dir):
            longp = os.path.join(dir, l)
            if os.path.isfile(longp):
                entries.append(('example', os.path.splitext(l)[0], longp))
        entries.sort()
        Submenu.__init__(self, ancestor, 'Examples', entries)
                
    def _event_example(self, widget, path):
        self.getAncestor('MainWindow').getChildren('Notebook').openSheet(path)

                
        